# -*- coding: utf-8 -*-
from google.appengine.ext import db
from ragendja.auth.models import User as BaseUser
import datetime
class User(BaseUser):
    uid = db.StringProperty(u"UID")
    origin = db.StringProperty(u"用户来源")
    nickname = db.StringProperty(u"昵称")
    tinyurl = db.LinkProperty(u"头像1")
    headurl = db.LinkProperty(u"头像2")
    
    loc = db.StringProperty(u"所在地")
    sex = db.StringProperty(u"性别")
    zidou = db.StringProperty(u"紫豆")
    
    watched_series_ids = db.StringListProperty(u"已观看视频ID")
    
    info_updated = db.DateTimeProperty(u"信息上次更新", auto_now_add = True)
    
    def watched_series_ids_display(self):
        return u" ".join(self.watched_series_ids)

    
    def __unicode__(self):
        return self.nickname
    
    @property
    def page(self):
        if self.origin == 'xiaonei':
            return u"http://xiaonei.com/profile.do?id=%s" % self.uid
        elif self.origin == '51':
            return u"http://home.51.com/%s" % self.uid

class Series(db.Model):
    name = db.StringProperty(u"连续剧名称")
    xiaoli_category_id = db.StringProperty(u"xiaoli Category ID")
    xiaoli_series_id = db.StringProperty(u"xiaoli Series ID")
    views_count = db.IntegerProperty(u"观看人数", default = 0)
    
    def __unicode__(self):
        return self.name
    
class Episode(db.Model):
    series = db.ReferenceProperty(Series, verbose_name =u"剧集", collection_name = 'episodes')
    xiaoli_category_id = db.StringProperty(u"xiaoli Category ID")
    xiaoli_series_id = db.StringProperty(u"xiaoli Series ID")
    series_name = db.StringProperty(u"连续剧名称")
    xiaoli_slug = db.StringProperty(u"xiaoli Slug")
    name = db.StringProperty(u"视频名称")
    watched_xn_users = db.ListProperty(db.Key)
    watched_51_users = db.ListProperty(db.Key)
    views_count = db.IntegerProperty(u"观看人数", default = 0)
    
    def watched_xn_users_display(self):
        return u" ".join(user.username for user in db.get(self.watched_xn_users))

    def watched_51_users_display(self):
        return u" ".join(user.username for user in db.get(self.watched_51_users))

    
    def add_watched(self, user):
        user_key = user.key()
        if user.origin == 'xiaonei':
            if user_key in self.watched_xn_users:self.watched_xn_users.remove(user_key)
            self.watched_xn_users.insert(0,user_key)
            self.watched_xn_users = self.watched_xn_users[:10]
            
        elif user.origin == '51':
            if user_key in self.watched_51_users:self.watched_51_users.remove(user_key)
            self.watched_51_users.insert(0,user_key)
            self.watched_51_users = self.watched_51_users[:10]
        
        xiaoli_series_id = str(self.xiaoli_series_id)
        if xiaoli_series_id in user.watched_series_ids:user.watched_series_ids.remove(xiaoli_series_id)
        user.watched_series_ids.insert(0,xiaoli_series_id)
        user.watched_series_ids = user.watched_series_ids[:30]
    
    def get_watched_user(self, user):
        if user.origin == 'xiaonei':
            return db.get(self.watched_xn_users)
        elif user.origin == '51':
            return db.get(self.watched_51_users)

        
    def __unicode__(self):
        return self.name
        

class Comment(db.Model):
    user = db.ReferenceProperty(User, verbose_name =u"用户", collection_name = 'comments')
    origin = db.StringProperty(u"用户来源")
    content = db.TextProperty(u"讨论内容")
    created = db.DateTimeProperty(u"创建时间", auto_now_add = True)
    series = db.ReferenceProperty(Series, verbose_name =u"评论剧集", collection_name = 'comments')
    episode = db.ReferenceProperty(Episode, verbose_name =u"评论视频", collection_name = 'comments')
        
    def __unicode__(self):
        return u"%s: %s" % (self.user, self.content[:30])
    
    def created_cn(self):
        seconds = 8*60*60
        return self.created + datetime.timedelta(seconds=seconds)

#from django.db import models
#from django.db.models import Q
#from django.contrib.auth.models import User
#from django.template.defaultfilters import slugify
#from django.contrib.contenttypes.models import ContentType
#from django.contrib.contenttypes import generic
#from django.db.models import signals
#from django.core.exceptions import ObjectDoesNotExist
#from django.core.files.storage import default_storage
#from django.conf import settings
##from customs.profile import Profile
##from customs.fields import BigIntegerField, SpaceSeparatedIntegerField
#
#import datetime
#import os
#import re
#import time
#import random
#from urllib import quote
#    
#from main.settings import *

#class SNSProfile(Profile):
#    uid = models.CharField(max_length = 255, blank = True, db_index = True)
#    origin = models.CharField(u"用户来源", max_length = 30, choices = USER_ORIGIN, blank = True)
#    nickname = models.CharField(u"昵称", max_length = 255, blank = True)
#    tinyurl = models.URLField(verify_exists = False, default = '', blank = True)
#    headurl = models.URLField(verify_exists = False, default = '', blank = True)
#    
#    university = models.CharField(u"所在地", max_length = 255, blank = True)
#    
#    sex = models.SmallIntegerField(u"性别", default = 1, choices = SEX)
#    zidou = models.BooleanField(u"紫豆", default = False)
#    
#    watched_series_ids = models.TextField(u"已观看视频ID", blank = True)
#    
#    info_updated = models.DateTimeField(u"信息更新", default=datetime.datetime.now)
#    
##    watched_episode_ids = models.TextField(u"已观看视频ID", blank = True)
#    
#    @property
#    def page(self):
#        if self.origin == 'xiaonei':
#            return u"http://xiaonei.com/profile.do?id=%s" % self.uid
#        elif self.origin == '51':
#            return u"http://home.51.com/%s" % self.uid
#            
#class Series(models.Model):
#    name = models.CharField(u"连续剧名称", max_length = 255)
#    xiaoli_category_id = models.IntegerField(u"xiaoliCategoryID", default = 0)
#    xiaoli_id = models.IntegerField(u"xiaoliID", db_index = True)
##    watched_xn_user_ids = models.TextField(u"已观看者xnID", blank = True)
##    watched_51_user_ids = models.TextField(u"已观看者51ID", blank = True)
#    views_count = models.IntegerField(u"观看人数", default = 0)
#    
#    def __unicode__(self):
#        return self.name
#        
#    class Meta:
#        verbose_name = u"剧集"
#        verbose_name_plural = u"剧集"
#    
#class Episode(models.Model):
#    series = models.ForeignKey(Series, verbose_name =u"剧集", related_name = 'episodes')
#    xiaoli_category_id = models.IntegerField(u"xiaoliCategoryID", default = 0)
#    xiaoli_series_id = models.IntegerField(u"xiaoli剧集ID", db_index = True)
#    series_name = models.CharField(u"剧集名称", max_length = 255)
#    xiaoli_slug = models.CharField(u"xiaoli ID", max_length = 25, db_index = True)
#    name = models.CharField(u"视频名称", max_length = 255)
#    watched_xn_user_ids = models.TextField(u"已观看者xnID", blank = True)
#    watched_51_user_ids = models.TextField(u"已观看者51ID", blank = True)
#    views_count = models.IntegerField(u"观看人数", default = 0)
#    
#    def add_watched(self, user):
#        user_id = str(user.id)
#        if user.origin == 'xiaonei':
#            id_list = [id for id in self.watched_xn_user_ids.split() if id][:20]
#            print id_list
#            if user_id in id_list:id_list.remove(user_id)
#            id_list.insert(0,user_id)
#            print id_list
#            self.watched_xn_user_ids = " ".join(id_list)
#            
#        elif user.origin == '51':
#            id_list = [id for id in self.watched_51_user_ids.split() if id][:20]
#            if user_id in id_list:id_list.remove(user_id)
#            id_list.insert(0,user_id)
#            self.watched_51_user_ids = " ".join(id_list)
#        
#        xiaoli_series_id = str(self.xiaoli_series_id)
#        id_list = [id for id in user.watched_series_ids.split() if id][:30]
#        if xiaoli_series_id in id_list:id_list.remove(xiaoli_series_id)
#        id_list.insert(0,xiaoli_series_id)
#        user.watched_series_ids = " ".join(id_list)
#    
#    def get_watched_user(self,user):
#        if user.origin == 'xiaonei':
#            id_list = [id for id in self.watched_xn_user_ids.split() if id]
#        elif user.origin == '51':
#            id_list = [id for id in self.watched_51_user_ids.split() if id]
#            
#        return User.objects.filter(id__in = id_list)
#    
#    def save(self, force_insert=False, force_update=False):
#        self.xiaoli_category_id = self.series.xiaoli_category_id
#        self.xiaoli_series_id = self.series.xiaoli_id
#        self.series_name = self.series.name
#        super(Episode, self).save(force_insert, force_update)
#        
#    def __unicode__(self):
#        return self.name
#        
#    class Meta:
#        verbose_name = u"视频"
#        verbose_name_plural = u"视频"
#        
#class Comment(models.Model):
#    user = models.ForeignKey(User, verbose_name = u"用户", related_name = 'comments')
#    origin = models.CharField(u"用户来源", max_length = 30, choices = USER_ORIGIN, blank = True)
##    reply_to = models.ForeignKey('self', verbose_name = u"回复给", related_name = 'replies', null = True, blank = True)
##    reply_to_user = models.ForeignKey(User, verbose_name = u"回复给用户", related_name = 'replies', null = True, blank = True)
##    replies_count = models.IntegerField(u"回复数", default = 0)
#    content = models.TextField(u"讨论内容")
#    created = models.DateTimeField(u"创建时间", auto_now_add = True)
#    series = models.ForeignKey(Series, verbose_name =u"评论剧集", related_name = 'comments', null = True, blank = True)
#    episode = models.ForeignKey(Episode, verbose_name =u"评论视频", related_name = 'comments', null = True, blank = True)
#    
#    
##    def save(self, force_insert=False, force_update=False):
##        self.origin = self.user.origin
##        if self.reply_to:
##            self.reply_to_user = self.reply_to.user
##            self.reply_to.replies_count = self.reply_to.replies_count + 1
##            self.reply_to.save()
##        super(Comment, self).save(force_insert, force_update)
#        
#    def __unicode__(self):
#        return u"%s: %s" % (self.user, self.content[:30])
#        
#    class Meta:
#        verbose_name = u"评论"
#        verbose_name_plural = u"评论"